$(function () {
    function UserRoles() {
        this.$saveBtn = $('#save-btn');
        // 初始化
        this.init();
    }

    UserRoles.prototype.Constructor = UserRoles;

    UserRoles.prototype.init = function () {
        this.registEvent();
    };

    UserRoles.prototype.registEvent = function () {
        var that = this;
        $('.view-res').click(function () {
            var self = $(this);
            self.parents('table').find('tr.hide').removeClass('hide');
            self.remove();
        });

        that.$saveBtn.click(function (e) {
            var roles = $('input[name=role]:checked');
            if (!roles || roles.length == 0) {
                BootBox.warning('请选择要赋予用户的角色');
                return false;
            }
            var roleIds = roles.map(function () {
                return $(this).attr('roleid');
            }).get().join(",");
            var userId = $('#userId').val();
            SysTool.Ajax.ajaxJsonCall('users/' + userId + '/role/save', {
                roleIds : roleIds
            }, function (data) {
                BootBox.success('保存成功！', function () {
                    window.location.href = "/users/home";
                });
            })
        });
    };

    window.UserRoles = new UserRoles();
});